package com.one.consumer.main.ui.fragment;

import android.content.DialogInterface;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AlertDialog;
import android.view.View;

import com.one.common.config.OrderState;
import com.one.common.config.RouterPath;
import com.one.common.manager.RouterManager;
import com.one.common.manager.event.Subscribe;
import com.one.common.model.event.CommonEvent;
import com.one.common.model.extra.DefaultExtra;
import com.one.common.view.base.IListView;
import com.one.common.view.pagestate.listpage.BaseListFragment;
import com.one.consumer.R;
import com.one.consumer.main.model.item.OrderItem;
import com.one.consumer.main.presenter.OrderListPresenter;
import com.one.consumer.main.ui.binder.OrderBinder;
import com.one.consumer.order.model.item.OrderEmptyItem;
import com.one.consumer.order.ui.binder.OrderEmptyBinder;

import butterknife.OnClick;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/21 上午10:45
 * description ：订单列表
 **/
public class OrderFragment extends BaseListFragment<OrderListPresenter> implements IListView, TabLayout.BaseOnTabSelectedListener,
        OrderBinder.OrderClickListener{
    private String orderState = OrderState.ALL;
    private TabLayout tlOrder;
    private static final String[] TAB_TITLE = {"全部订单", "待指派", "待上门", "待确认", "待付款", "已完成","已取消订单"};

    @Override
    protected void initPresenter() {
        mPresenter = new OrderListPresenter(this, getActivity());
    }

    @Override
    public void registerDate() {
        register(OrderItem.class, new OrderBinder(this));
        register(OrderEmptyItem.class, new OrderEmptyBinder());
    }

    @Override
    public void loadData() {
        mPresenter.getOrderList(orderState);
    }

    @Override
    public void initView() {
        super.initView();
        addTopView(R.layout.layout_order_list_top);
        tlOrder = successView.findViewById(R.id.tl_order);
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[0]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[1]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[2]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[3]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[4]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[5]));
        tlOrder.addTab(tlOrder.newTab().setText(TAB_TITLE[6]));
        tlOrder.addOnTabSelectedListener(this);

        successView.findViewById(R.id.iv_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterManager.getInstance().go(RouterPath.SEARCH_ORDER);
            }
        });
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        switch (tab.getPosition()) {
            case 0:
                orderState = OrderState.ALL;
                break;
            case 1:
                orderState = OrderState.WAIT_ASSIGNED;
                break;
            case 2:
                orderState = OrderState.WAIT_VISIT;
                break;
            case 3:
                orderState = OrderState.WAIT_CONFIRM;
                break;
            case 4:
                orderState = OrderState.WAIT_PAY;
                break;
            case 5:
                orderState = OrderState.PAY_END;
                break;
            case 6:
                orderState = OrderState.CANCEL;
                break;
            default:
                orderState = OrderState.ALL;
                break;
        }
        doRefresh();
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public boolean onEmpty() {
        addData(new OrderEmptyItem());
        return true;
    }

    @Override
    public void onResume() {
        super.onResume();
        doRefresh();
    }

    /**
     * 取消订单
     */
    @Override
    public void cancelOrder(OrderItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("确定取消订单？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mPresenter.cancelOrder(item.getOrder_id()+"");
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                }).show();

    }

    /**
     * 删除订单
     * @param item
     */
    @Override
    public void deleteOrder(OrderItem item) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("确定删除订单？")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mPresenter.deleteOrder(item.getOrder_id()+"");
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.dismiss();
                    }
                }).show();

    }

    /**
     * 支付
     */
    @Override
    public void onPay(OrderItem item) {
//        mPresenter.payOrder(item.getOrder_id()+"");
//        RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage("您确定已知维修报价并需要维修？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mPresenter.payOrder(item.getOrder_id()+"");
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builder.show();
    }

    /**
     * 查看详情
     */
    @Override
    public void onDetail(OrderItem item) {
        RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
    }

    /**
     * 立即确认
     */
    @Override
    public void onConfirm(OrderItem item) {
//
//        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
//        builder.setMessage("您确定已知维修报价并需要维修？");
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
                RouterManager.getInstance().go(RouterPath.ORDER_DETAIL, new DefaultExtra(item.getOrder_id() + ""));
//            }
//        });
//
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//
//            }
//        });
//
//        builder.show();
    }

    /**
     * 立即评价
     */
    @Override
    public void onEvaluation(OrderItem item) {
        // 跳转页面
        RouterManager.getInstance().go(RouterPath.ADD_COMMENT,
                new DefaultExtra(item.getOrder_id()+""));
    }

    /**
     * 收到推送更新列表
     */
    @Subscribe
    public void onJpush(CommonEvent event){
        if(event!=null){
            doRefresh();
        }
    }
}
